import esriLoader from 'esri-loader'
let esri = {
  "Map": "esri/map",
  "Basemap": "esri/Basemap",
  "MapView": "esri/views/MapView",
  "SceneView": "esri/views/SceneView",
  "TileLayer": 'esri/layers/TileLayer',
  "MapImageLayer": 'esri/layers/MapImageLayer',
  "Extent": 'esri/geometry/Extent',
  "watchUtils": 'esri/core/watchUtils',
  "BaseTileLayer": 'esri/layers/BaseTileLayer',
  "Color": 'esri/Color',
  "esriConfig": "esri/config",
  "esriRequest": "esri/request",
  "FeatureLayer": "esri/layers/FeatureLayer",
  "Graphic": "esri/Graphic",
  "GraphicsLayer": "esri/layers/GraphicsLayer",
  "Point": "esri/geometry/Point",
  "SceneLayer": "esri/layers/SceneLayer",
  "SimpleFillSymbol": "esri/symbols/SimpleFillSymbol",
  "LabelClass": "esri/layers/support/LabelClass",
  "LayerList": "esri/widgets/LayerList",
  "Geoprocessor": "esri/tasks/Geoprocessor",
  "ImageParameters": "esri/layers/support/ImageParameters",
  "Multipoint": "esri/geometry/Multipoint",
  "LinearUnit": "esri/tasks/support/LinearUnit",
  "FeatureSet": "esri/tasks/support/FeatureSet",
  "HeatmapRenderer": "esri/renderers/HeatmapRenderer",
  "Field": "esri/layers/support/Field",
  "QueryTask": "esri/tasks/QueryTask",
  "Query": "esri/tasks/support/Query",
  "SpatialReference": "esri/geometry/SpatialReference",
  "GeometryService": "esri/tasks/GeometryService",
  "AreasAndLengthsParameters": "esri/tasks/support/AreasAndLengthsParameters",
  "ProjectParameters": "esri/tasks/support/ProjectParameters",
  "ImageryLayer": "esri/layers/ImageryLayer",
  "RasterFunction": "esri/layers/support/RasterFunction",
  "DimensionalDefinition": "esri/layers/support/DimensionalDefinition",
  "MosaicRule": "esri/layers/support/MosaicRule",
  "ElevationLayer": "esri/layers/ElevationLayer",
  "BasemapToggle": "esri/widgets/BasemapToggle",
  "ScaleBar": "esri/widgets/ScaleBar",
  "Sketch": "esri/widgets/Sketch",
  "AreaMeasurement2D": "esri/widgets/AreaMeasurement2D",
  "Fullscreen":"esri/widgets/Fullscreen",
  "webMercatorUtils":"esri/geometry/support/webMercatorUtils",
  "externalRenderers":"esri/views/3d/externalRenderers",
  "domReady": "dojo/domReady!"
};

/**
 * 加载arcgisAPI
 * @function loadAPI
 * @param {String} arcgisJS arcgisAPI地址
 * @param {String} arcgisCSS  arcgis样式地址
 * @returns {Promise<any>}
 */
function loadArcgisAPI(arcgisJS, arcgisCSS,loadApiEnd) {
  if (esriLoader.isLoaded()) {
    console.error("该arcgisAPI模块已经加载过了");
    loadApiEnd && loadApiEnd();
    return;
  }
  // 加载css
  esriLoader.loadCss(arcgisCSS);
  //加载js
  const options = {
    url: arcgisJS,
    dojoConfig: {
      async: true
    },
  };
  esriLoader.loadScript(options).then(() => {
    loadModulesSplit(loadApiEnd);
  }).catch(error => {
    console.error("arcgisAPI加载失败，请检查API地址是否正确", error);
  })

}

function loadModulesSplit(loadApiEnd) {
  loadModules(esri, loadApiEnd);
}
function loadModules(config, callBack) {
  let pathArr = [], keyArr = [];
  for (let key in config) {
    keyArr.push(key);
    pathArr.push(config[key]);
  }
  esriLoader.loadModules(pathArr).then((params) => {
    for (var index in keyArr) {
      let obj = keyArr[index];
      esri[obj] = params[index]
    }
    callBack && callBack();
  })
}



export {esri, esriLoader,loadArcgisAPI};
